﻿using HomagChina.CCHCS.DataTypeModel;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

public class ExcelDataReader
{
    public ObservableCollection<Proxy.LayerInfo> BoardsList = new ObservableCollection<Proxy.LayerInfo>()
    {
        new Proxy.LayerInfo() { Boardcode = "GZZHCS_10001", Length = 850, Width = 392, Thickness = 18, CalCenterX = 0, RSCenterY = 0, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=0 },
        new Proxy.LayerInfo() { Boardcode = "GZZHCS_10001", Length = 850, Width = 392, Thickness = 18, CalCenterX = 0, RSCenterY = 395, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=36 },
        new Proxy.LayerInfo() { Boardcode = "GZZHCS_10001", Length = 392, Width = 50, Thickness = 18, CalCenterX = 0, RSCenterY = 800, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=54 },
        new Proxy.LayerInfo() { Boardcode = "GZZHCS_10001", Length = 392, Width = 96, Thickness = 18, CalCenterX = 0, RSCenterY = 860, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=54 },
        new Proxy.LayerInfo() { Boardcode = "GZZHCS_10001", Length = 2800, Width =800, Thickness = 70, CalCenterX = 0, RSCenterY = 0, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = false,ZCoor=54 },

        //new LayerInfo() { Boardcode = "GZZHCS_10001", Length = 850, Width = 392, Thickness = 18, CalCenterX = 425, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=18 },
        //new LayerInfo() { Boardcode = "GZZHCS_10001", Length = 850, Width = 392, Thickness = 18, CalCenterX = 425, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=36 },
        //new LayerInfo() { Boardcode = "GZZHCS_10001", Length = 850, Width = 392, Thickness = 18, CalCenterX = 425, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 1, PackRt90 = false,ZCoor=54 },
        //new LayerInfo() { Boardcode = "GZZHCS_10002", Length = 850, Width = 392, Thickness = 18, CalCenterX = 425, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 4, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_10003", Length = 392, Width = 50, Thickness = 18, CalCenterX = 25, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_10004", Length = 392, Width = 96, Thickness = 18, CalCenterX = 100, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_10005", Length = 392, Width = 146, Thickness = 18, CalCenterX = 224, RSCenterY = 196, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_10006", Length = 550, Width = 96, Thickness = 18, CalCenterX = 575, RSCenterY = 48, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_10007", Length = 550, Width = 96, Thickness = 18, CalCenterX = 575, RSCenterY = 145, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_10008", Length = 550, Width = 96, Thickness = 18, CalCenterX = 575, RSCenterY = 245, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_10009", Length = 550, Width = 96, Thickness = 18, CalCenterX = 575, RSCenterY = 344, RSShelfOrder = 1, RSLayerNum = 2, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_100010", Length = 850, Width = 50, Thickness = 18, CalCenterX = 425, RSCenterY = 25, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_100011", Length = 850, Width = 96, Thickness = 18, CalCenterX = 425, RSCenterY = 100, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = false },
        //new LayerInfo() { Boardcode = "GZZHCS_100012", Length = 212, Width = 240, Thickness = 18, CalCenterX = 120, RSCenterY = 272, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_100013", Length = 212, Width = 240, Thickness = 18, CalCenterX = 347, RSCenterY = 272, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_100014", Length = 240, Width = 212, Thickness = 18, CalCenterX = 531, RSCenterY = 272, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = true },
        //new LayerInfo() { Boardcode = "GZZHCS_100015", Length = 240, Width = 212, Thickness = 18, CalCenterX = 744, RSCenterY = 272, RSShelfOrder = 1, RSLayerNum = 3, PackRt90 = true },

    //    new LayerInfo() { Boardcode = "GZZHCS_100016", Length = 720, Width = 392, Thickness = 18, CalCenterX = 360, RSCenterY = 196, RSShelfOrder = 3, RSLayerNum = 1, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100017", Length = 720, Width = 392, Thickness = 18, CalCenterX = 360, RSCenterY = 196, RSShelfOrder = 3, RSLayerNum = 4, PackRt90 = false },

    //    new LayerInfo() { Boardcode = "GZZHCS_100018", Length = 550, Width = 96, Thickness = 18, CalCenterX = 275, RSCenterY = 48, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100019", Length = 550, Width = 96, Thickness = 18, CalCenterX = 275, RSCenterY = 145, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100020", Length = 550, Width = 96, Thickness = 18, CalCenterX = 275, RSCenterY = 245, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100021", Length = 550, Width = 96, Thickness = 18, CalCenterX = 275, RSCenterY = 344, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100022", Length = 392, Width = 116, Thickness = 18, CalCenterX = 576, RSCenterY = 196, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = true },
    //    new LayerInfo() { Boardcode = "GZZHCS_100023", Length = 392, Width = 120, Thickness = 18, CalCenterX = 662, RSCenterY = 196, RSShelfOrder = 3, RSLayerNum = 2, PackRt90 = true },

    //    new LayerInfo() { Boardcode = "GZZHCS_100024", Length = 720, Width = 120, Thickness = 18, CalCenterX = 360, RSCenterY = 332, RSShelfOrder = 3, RSLayerNum = 3, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100025", Length = 720, Width = 120, Thickness = 18, CalCenterX = 360, RSCenterY = 211, RSShelfOrder = 3, RSLayerNum = 3, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100026", Length = 250, Width = 150, Thickness = 18, CalCenterX = 125, RSCenterY = 75, RSShelfOrder = 3, RSLayerNum = 3, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100027", Length = 468, Width = 96, Thickness = 18, CalCenterX =486 , RSCenterY = 100, RSShelfOrder = 3, RSLayerNum = 3, PackRt90 = false },
    //    new LayerInfo() { Boardcode = "GZZHCS_100028", Length = 468, Width = 50, Thickness = 18, CalCenterX = 486, RSCenterY = 25, RSShelfOrder = 3, RSLayerNum = 3, PackRt90 = false }
    };

    public ObservableCollection<Proxy.LayerInfo> ReadFromExcel(string filePath = @"C:\Users\tb28who\Desktop\Homag\Homag Project Info\Under development\Horizontal Sorting\CCHCS\HomagChina.CCHCS\HomagChina.CCHCS\ViewModels\Home\SortingModule\Data\广州展会水平分拣工件清单20250313.xlsx")
    {
        var list = new ObservableCollection<Proxy.LayerInfo>();
        FileInfo fileInfo = new FileInfo(filePath);

        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

        using (var package = new ExcelPackage(fileInfo))
        {
            var worksheet = package.Workbook.Worksheets[0];

            const int colLength = 1;
            const int colWidth = 2;
            const int colThickness = 3;
            const int colBarcode = 5;
            const int colLayerNum = 7;
            const int colCenterX = 8;
            const int RSShelfOrder = 6;
            const int colCenterY = 9;
            const int colRotation = 11;
            const int ZCoor = 10;
            for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
            {
                try
                {
                    var item = new Proxy.LayerInfo
                    {
                        Name = worksheet.Cells[row, colBarcode].Text.Trim(),
                        Length = GetFloatValue(worksheet.Cells[row, colLength]),
                        Width = GetFloatValue(worksheet.Cells[row, colWidth]),
                        Thickness = GetFloatValue(worksheet.Cells[row, colThickness]),
                        Boardcode = worksheet.Cells[row, colBarcode].Text.Trim(),
                        RSShelfOrder = GetIntValue(worksheet.Cells[row, RSShelfOrder]),
                        RSLayerNum = GetIntValue(worksheet.Cells[row, colLayerNum]),
                        CalCenterX = GetFloatValue(worksheet.Cells[row, colCenterX]),
                        RSCenterY = GetFloatValue(worksheet.Cells[row, colCenterY]),
                        ZCoor = GetFloatValue(worksheet.Cells[row, ZCoor]),
                        PackRt90 = GetRotationValue(worksheet.Cells[row, colRotation])
                    };

                    list.Add(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"第{row}行数据读取失败: {ex.Message}");
                }
            }
        }
        return list;
    }

    private float? GetFloatValue(ExcelRange cell)
    {
        if (double.TryParse(cell.Text, out double result))
            return (float)Math.Round(result, 2);
        return null;
    }

    private int GetIntValue(ExcelRange cell)
    {
        return int.TryParse(cell.Text, out int result) ? result : 0;
    }

    private bool GetRotationValue(ExcelRange cell)
    {
        // 旋转列逻辑：0-不旋转，90-旋转
        return GetIntValue(cell) == 90;
    }
}